package com.say.receiver;

import com.say.tool.Tool;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.widget.Toast;

public class WifiStateReceiver extends BroadcastReceiver {
	// android.net.wifi.STATE_CHANGE
	// android.net.wifi.WIFI_STATE_CHANGED
	Context context;
	public void onReceive(Context context, Intent intent) {
		this.context = context;
		String action = intent.getAction();
		if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) {
			// signal strength changed
			toast("接收信号强度指示器");
		} else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {// wifi连接上与否
			toast("网络状态改变");
			NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
			if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
				toast("wifi网络连接断开");
			} else if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
				WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
				WifiInfo wifiInfo = wifiManager.getConnectionInfo();
				// 获取当前wifi名称
				toast("连接到网络: " + wifiInfo.getSSID());
				Tool.logInfo(wifiInfo);
			}
		} else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {// wifi打开与否
			int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
			if (wifistate == WifiManager.WIFI_STATE_DISABLED) {
				toast("系统关闭wifi");
			} else if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
				toast("系统开启wifi");
			}
		}
	}
	private void toast(String str) {
		Toast.makeText(context, str, Toast.LENGTH_LONG).show();
		Tool.logInfo(str);
	}
}
